package com.magina.calendario;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import java.util.Calendar;

/**
 * Created by IntelliJ IDEA.
 * User: marcioat
 * Date: 27/05/11
 * Time: 17:40
 */
public class frmNovoEvento  extends ListActivity
{
    private Button mPickDate;

    private Cursor mCursor;
    private int mYear;
    private int mMonth;
    private int mDay;

    static final int DATE_DIALOG_ID = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        Calendar mcal = Calendar.getInstance();

        mYear = mcal.get(Calendar.YEAR);
        mMonth =  mcal.get(Calendar.MONTH);
        mDay = mcal.get(Calendar.DAY_OF_MONTH);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.frm_evento_novo);

        Button btnGravar;
        btnGravar = (Button) findViewById(R.id.btnGetCategoria);
        btnGravar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.YEAR, mYear);
                cal.set(Calendar.MONTH, mMonth);
                cal.set(Calendar.DAY_OF_MONTH, mDay);
                long dateMili = cal.getTimeInMillis();

                addEvento(mCursor.getString(1), dateMili);
                mPickDate.setText("Selecione a data");
                setResult(RESULT_OK);
                //finish();
            }
        });

        Button btnSair;
        btnSair = (Button) findViewById(R.id.btnGravar);
        btnSair.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setResult(RESULT_OK);
                finish();
            }
        });

        mPickDate = (Button) findViewById(R.id.btnPegarData);
        mPickDate.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        mCursor = this.getContentResolver().query(CategoriaProvider.Categoria.CONTENT_URI, null, null, null, null);
        this.startManagingCursor(mCursor);

        ListAdapter adapter = new SimpleCursorAdapter(
        		              // O primeiro parametro eh o context.
        		              this,
        		              // O segundo, o layout de cada item.
        		              R.layout.list_item,
        		              // O terceiro parametro eh o cursor que contem os dados
        		              // a serem mostrados
        		              mCursor,
        		              // o quarto parametro eh um array com as colunas do cursor
        		              // que serao mostradas
        		              new String[] {CategoriaProvider.Categoria.TEXT},
        		              // o quinto parametro eh um array (com o mesmo tamanho
        		              // do anterior) com os elementos que receberao
        		              // os dados.
        		              new int[] {R.id.txtCategoGrid});


          setListAdapter(adapter);

        ListView lstView;
        lstView = getListView();
        lstView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {

                    }
                }
        );
    }

    // updates the date in the TextView
    private void updateDisplay()
    {
        String strMes = "0" + String.valueOf(mMonth+1);
        strMes = strMes.substring(strMes.length()-2);

        String strDia = "0" + String.valueOf(mDay);
        strDia = strDia.substring(strDia.length()-2);

        String strData;
        strData = strDia + "/" + strMes + "/" + String.valueOf(mYear);

        mPickDate.setText(strData);
    }

    // the  received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
            };


    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
//            break;
//            case DATE_DIALOG_ID_X:
//                return dialog;
        }
        return null;
    }

    protected void addEvento(String text, long dat_evento)
    {
        ContentValues values = new ContentValues();
        values.put(EventoProvider.Evento.TEXT, text);
        values.put(EventoProvider.Evento.DATA_EVE, dat_evento);
        getContentResolver().insert(EventoProvider.Evento.CONTENT_URI, values);
    }
}
